package cn.school.dailyWork.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.school.attendance.dormitory.dao.DormitoryAttendanceMapper;
import cn.school.attendance.dormitory.dao.DormitoryStudentInfoMapper;
import cn.school.attendance.dormitory.entity.DormitoryAttendanceDo;
import cn.school.attendance.dormitory.entity.DormitoryStudentInfo;
import cn.school.common.utils.SysUserUtils;
import cn.school.dailyWork.dao.StudentEntryRecodeDao;
import cn.school.dailyWork.entity.AttendanceRecordsEntity;
import cn.school.dailyWork.service.FaceGateService;
import cn.school.dailyWork.vo.FaceGateFlow;
import com.alibaba.excel.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @className: FaceGateServiceImpl
 * @author: Alex
 * @date: 2023/6/1
 **/
@Slf4j
@Service
public class FaceGateServiceImpl extends ServiceImpl<StudentEntryRecodeDao, FaceGateFlow> implements FaceGateService {
	@Resource
	private DormitoryAttendanceMapper attendanceMapper;
	
	@Resource
	private DormitoryStudentInfoMapper dormitoryStudentInfoMapper;
	@Override
	public Boolean saveFaceGateFlowList(List<Map<String, String>> flowList) throws ParseException {
		if (CollectionUtil.isEmpty(flowList)) {
			return true;
		}
		List<FaceGateFlow> faceGateFlows = new ArrayList<>();
		for (Map<String, String> map : flowList) {
			FaceGateFlow gateFlow = new FaceGateFlow();
			gateFlow.setStudentId(map.get("studentId"));
			String entryTime = map.get("entryTime");
			
			DateTime parse = DateUtil.parse(entryTime, "yyyy-MM-dd HH:mm:ss");
			String format = DateUtil.format(parse, "yyyy/MM/dd HH:mm:ss");
			gateFlow.setEntryTime(DateUtils.parseDate(format));
			gateFlow.setSnapUrl("//faceImage/" + map.get("studentId") + ".jpg");
			faceGateFlows.add(gateFlow);
		}
		// 存入数据库
		return saveBatch(faceGateFlows);
	}
	
	@Override
	public Boolean saveAttendanceRecords(List<Map<String, String>> records) throws ParseException {
		if (CollectionUtil.isEmpty(records)) {
			return false;
		}
		
		List<AttendanceRecordsEntity> recordsEntityList = new ArrayList<>();
		// 根据学生身份证id 查找出位置id 和租户id
		for (Map<String, String> record : records) {
			AttendanceRecordsEntity recordsEntity = new AttendanceRecordsEntity();
			recordsEntity.setStudentId(record.get("studentId"));
			recordsEntity.setClockInTime(DateUtils.parseDate(record.get("clockInTime")));
			recordsEntity.setIdCard(record.get("IdCard"));
			recordsEntity.setTelephone(record.get("telephone"));
			recordsEntityList.add(recordsEntity);
		}
		
		// 学生身份证id
		Set<String> collect = recordsEntityList.stream().map(AttendanceRecordsEntity::getTelephone).collect(Collectors.toSet());
		
		LambdaQueryWrapper<DormitoryStudentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.in(DormitoryStudentInfo::getPhone, collect);
		List<DormitoryStudentInfo> studentInfos = dormitoryStudentInfoMapper.selectList(lambdaQueryWrapper);
		if (CollectionUtil.isEmpty(studentInfos)) {
			log.info("没有查找到学生信息。。。");
			return false;
		}
		
		for (AttendanceRecordsEntity recordsEntity : recordsEntityList) {
			Optional<DormitoryStudentInfo> first = studentInfos.stream().filter(dormitoryStudentInfo -> Objects.equals(dormitoryStudentInfo.getCertificateId(), recordsEntity.getIdCard())).findFirst();
			if (!first.isPresent()) {
				continue;
			}
			DormitoryStudentInfo info = first.get();
			// 保存考勤记录 已打卡
			DormitoryAttendanceDo attendanceDo = new DormitoryAttendanceDo();
			attendanceDo.setStudentId(info.getStudentId());
			attendanceDo.setLocationId(info.getLocationId());
			attendanceDo.setStatus("1");
			attendanceDo.setCreateTime(DateUtils.format(recordsEntity.getClockInTime()));
			attendanceDo.setCreateBy(SysUserUtils.getSysUser().getUsername());
			attendanceDo.setTenantId(String.valueOf(info.getTenantId()));
			
			attendanceMapper.insert(attendanceDo);
		}
		return true;
	}
}
